package logic

import (
	"context"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/services/wallet/rpc/walletClient"

	"xialou_platform/services/wallet/api/internal/svc"
	"xialou_platform/services/wallet/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type WalletInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewWalletInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) WalletInfoLogic {
	return WalletInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WalletInfoLogic) WalletInfo(req *types.WalletInfoReq) (resp *types.WalletInfoResp, err error) {
	uid, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return nil, err
	}
	var sourceId int64
	switch req.Platform {
	case "xiaoke":
		sourceId = uid
	case "xiuxian":
		sourceId = respx.IDParse(req.BusinessId).GetInt64()
	}

	walletInfo, err := l.svcCtx.WalletRpc.WalletInfo(l.ctx, &walletClient.WalletInfoReq{SourceId: sourceId})
	if err != nil {
		return nil, err
	}

	resp = &types.WalletInfoResp{
		Balance:         walletInfo.Balance,
		TodayProfit:     walletInfo.TodayProfit,
		YesterdayProfit: walletInfo.YesterdayProfit,
		Icon:            walletInfo.Icon,
		BankName:        walletInfo.BankName,
		BankNumber:      walletInfo.BankNumber,
		SaleCardIncome:  walletInfo.SaleCardIncome,
		CouponIncome:    walletInfo.CouponIncome,
	}

	return
}
